<?php

class newsController extends base
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取资讯列表
     * @return array
     */
    public function dogetNewsList()
    {
    	$point = common::request('news_id', 'G', 0);
    	$direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 10);
    	$is_push = common::request('is_push', 'G', 0);

        //统计 推送数据
        if($is_push == 1 && empty($point))
        {
            $pushData['time'] = time();
            $pushData['client'] = $this->client;
            $pushData['uid'] = $this->uid ? $this->uid : 0;
            $pushData['projectId'] = $this->projectId;
            $pushData['version'] = $this->version;
            $pushData['push_type'] = 'newslist';
            $pushData['push_value'] = 0;
            $this->load('news')->pushWorkRecord($pushData);
        }
        $newsData = $this->load('news')->getNewsList($point, $direc, $offset);

    	$this->result['result'] = $newsData;
    }
    /**
     * 获取某资讯详情
     * @return array
     */
    public function dogetNewsInfo()
    {
        $news_id = common::request('news_id', 'G', 0);
        if(empty($news_id))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $is_push = common::request('is_push', 'G', 0);
        //统计 点击数据
        $this->load('news')->makeNewsClickStat($news_id, $is_push);

        $newsInfo = $this->load('news')->getNewsInfo($news_id);
        if($newsInfo)
        {
            !isset($newsInfo['total_comment']) && $newsInfo['total_comment'] = 0;
        }
        $hot_comments = $this->load('news')->getHotComments($news_id);
        if($hot_comments)
        {
            foreach($hot_comments as $key=> &$v)
            {
                $v['is_lighten'] = 0;
                if($this->uid)
                {
                    $v['is_lighten'] = $this->redis->hexists(REDIS_PREFIX.'NewsCommentLight'.$v['comment_id'], $this->uid) ? 1 : 0;
                }
            }
        }
        $this->result['result']['newsInfo'] = $newsInfo;
        $this->result['result']['hot_comments'] = $hot_comments;
    }
    /**
     * 获取资讯评论列表
     * @return array
     */
    public function dogetCommentList()
    {
    	$news_id = common::request('news_id', 'G');
    	$point = common::request('comment_id', 'G', 0);
    	$direc = common::request('direc', 'G', 'next');
    	$offset = common::request('offset', 'G', 10);

    	if(!$news_id)
    	{
    		$_ENV['error']->getErrors(101);
            exit;
    	}
        //获取热门评论
        $hotComments = $this->load('news')->getHotComments($news_id);
        if($hotComments)
        {
            foreach($hotComments as $key=> &$v)
            {
                $v['is_lighten'] = 0;
                !isset($v['thumb_size']) && $v['thumb_size'] = '0x0';
                if($this->uid)
                {
                    $v['is_lighten'] = $this->redis->hexists(REDIS_PREFIX.'NewsCommentLight'.$v['comment_id'], $this->uid) ? 1 : 0;
                }
            }
        }
        //获取所有评论
    	$allComments = $this->load('news')->getNewsCommentList($news_id, 'all', $point, $direc, $offset);
        if($allComments)
        {
            foreach($allComments as $key=> &$v)
            {
                $v['is_lighten'] = 0;
                if($this->uid)
                {
                    $v['is_lighten'] = $this->redis->hexists(REDIS_PREFIX.'NewsCommentLight'.$v['comment_id'], $this->uid) ? 1 : 0;
                }
            }
        }
        else
        {
            $allComments = array();
        }
        $this->result['result']['hotComments'] = $hotComments;
        $this->result['result']['allComments'] = $allComments;
        $this->result['result']['totalComments'] = $this->redis->hget(REDIS_PREFIX. 'NewsInfo_' . $news_id, 'total_comment');
    }
    /**
     * 点亮评论
     * @return array
     */
    public function dolightenComment()
    {
    	if(!$this->uid)
    	{
    		$_ENV['error']->getErrors(201);
    		exit;
    	}

    	$comment_id = common::request('comment_id', 'G');
    	if(!$comment_id)
    	{
    		$_ENV['error']->getErrors(101);
            exit;
    	}
    	$this->result['result']['is_success'] = $this->load('news')->lightenNewsComment($comment_id);
    }
    /**
     * 发布评论
     * @return array
     */
    public function dopublishComment()
    {
    	if(!$this->uid)
    	{
    		$_ENV['error']->getErrors(201);
    		exit;
    	}
    	$news_id = common::request('news_id', 'P');
        $comment_content = common::request('comment_content', 'P');
    	$father_id  = common::request('father_id ', 'P');
    	if(!$news_id || empty($comment_content))
    	{
    		$_ENV['error']->getErrors(101);
            exit;
    	}
        if ($this->redis->exists(REDIS_PREFIX.'NewsComment'.$this->uid))
        {
          $_ENV['error']->getErrors(607);
          exit();
        }
    	if($this->load('badwords')->check($comment_content))
    	{
    		$_ENV['error']->getErrors(605);
            exit;
    	}
        $comment_img = $this->load('qiniu')->uploadQiNiuImg('comment_img', 'NC/'.md5($news_id.'/'.time().common::getHashStr(6)));
        $this->redis->set(REDIS_PREFIX.'NewsComment'.$this->uid, time(), 30);
    	$this->result['result'] = $this->load('news')->publishNewsComment($news_id, $comment_content, $this->uid,$father_id, $comment_img);
    }
    /**
     * 收藏资讯
     * @return int is_success
     */
    public function docollectNews()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id', 'G');
        $uid = $this->uid;
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($postInfo) || $postInfo['is_show'] != 1 || !$uid)
        {
            $_ENV['error']->getErrors(101);
            exit();
        }
        if($this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id) == false)
        {
            if($_ENV['bbs']->collectPost($news_id, $uid))
            {
                $this->redis->zadd(REDIS_PREFIX.'CollectPostList_'.$uid, time(), $news_id);
            }
        }
        if($this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$news_id, $uid) !==false)
        {
            $is_success = 1;
        }
        else
        {
            $record_id = $_ENV['bbs']->likePost($news_id, $uid);
            $is_success = $record_id ? 1 : 0;
            if($is_success)
            {
                $this->redis->zadd(REDIS_PREFIX.'PostLikeList_'.$news_id, time(), $uid);
                $this->redis->zadd(REDIS_PREFIX.'MyLovePList_'.$uid, $record_id, $news_id);
                $this->db->update("update r_news set loves = loves+1 where news_id = '{$news_id}'");
                $this->redis->HINCRBY(REDIS_PREFIX. 'NewsInfo_'.$news_id, 'loves', 1);
                if($uid != $postInfo['uid'])
                {
                    $_ENV['bbs']->addUnReadMsg($postInfo['uid'], array('msgType'=>4, 'sponsor'=>array('uid'=>$uid,'comment_id'=>'0'), 'quote'=>array('news_id'=>$news_id,'comment_id'=>'0')));
                }
            }
        }

        $this->result['result']['is_success'] = $is_success;
        $this->result['result']['news_id'] = $news_id;
    }
    /**
     * 收藏资讯列表
     * @return int is_success
     */
    public function domyCollectList()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $record_id = common::request('collection_id', 'G', 0);
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 10);

        $this->result['result'] = $this->load('news')->getcollectNewsList($this->uid, $record_id, $direc, $offset);
    }
    /**
     * 取消收藏资讯
     * @return int is_success
     */
    public function docancelCollectNews()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id', 'G');
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($postInfo) || $postInfo['is_show'] != 1 || !$uid)
        {
            $_ENV['error']->getErrors(101);
            exit();
        }
        if($this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id) !== false)
        {
            if($_ENV['bbs']->discollectPost($news_id, $uid))
            {
                $this->redis->zadd(REDIS_PREFIX.'CollectPostList_'.$uid, time(), $news_id);
            }
        }
        if($this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$news_id, $uid) === false)
        {
            $is_success = 1;
        }
        else
        {
            $is_success = $_ENV['bbs']->unlikePost($news_id, $uid);
            if($is_success)
            {
                $this->db->update("update r_news set loves = loves-1 where news_id = '{$news_id}'");
                $this->redis->HINCRBY(REDIS_PREFIX. 'NewsInfo_'.$news_id, 'loves', -1);
                $this->redis->zrem(REDIS_PREFIX.'PostLikeList_'.$news_id, $uid);
                $this->redis->zrem(REDIS_PREFIX.'MyLovePList_'.$uid, $news_id);
            }
        }

        $this->result['result']['is_success'] = $is_success;
        $this->result['result']['news_id'] = $news_id;
    }
}